package com.framework.sch.job;

import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.SpringUtil;
import com.framework.bas.util.UtilMessage;
import com.framework.sch.bs.IPlmSchBS;
import com.framework.sch.util.PlmSchConstant;
import com.framework.sch.vo.HpSchTaskVO;
import com.framework.sog.bs.IPlmSogBS;
import com.framework.sog.util.PlmSogConstant;
import com.framework.sog.vo.HpSogTaskVO;

public abstract class AbsPlmSchJob implements Job {
	
	private static IPlmBasBS plmBasBS;
	private static IPlmSogBS plmSogBS;
	private static IPlmSchBS plmSchBS;

	public IPlmBasBS getPlmBasBS() {
		if(plmBasBS == null){
			try{
				plmBasBS = (IPlmBasBS)SpringUtil.getBean(IPlmBasBS.BEAN_ID);
			}catch(Exception e){}
		}
		return plmBasBS;
	}
	
	public IPlmSogBS getPlmSogBS() {
		if(plmSogBS == null){
			try{
				plmSogBS = (IPlmSogBS)SpringUtil.getBean(IPlmSogBS.BEAN_ID);
			}catch(Exception e){}
		}
		return plmSogBS;
	}
	
	public IPlmSchBS getPlmSchBS() {
		if(plmSchBS == null){
			try{
				plmSchBS = (IPlmSchBS)SpringUtil.getBean(IPlmSchBS.BEAN_ID);
			}catch(Exception e){}
		}
		return plmSchBS;
	}

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		if (this.getPlmBasBS()!= null && this.getPlmSogBS()!=null){
			String jobCode = arg0.getJobDetail().getName();
			HpSchTaskVO schTaskVO = this.getPlmSchBS().queryTaskObj(jobCode);
			GeneralParam param = GeneralParam.getCurrent();
			param.setValue(PlmSchConstant.TASK_OBJECT, schTaskVO);
			doJob(param);
		}
	}
	
	public final void taskBeforeLog(GeneralParam param) {
		HpSogTaskVO vo = new HpSogTaskVO();
		HpSchTaskVO schTaskVO = (HpSchTaskVO)param.getValueObject(PlmSchConstant.TASK_OBJECT);
		vo.setTaskCode(schTaskVO.getTaskCode());
		vo.setTaskName(schTaskVO.getTaskName());
		vo.setTaskStatus(PlmSchConstant.TASK_RUN_START);
		vo.setTaskType(schTaskVO.getTaskType());
		if (schTaskVO.getIsDataLog().equals(PlmBasConstant.STOP_FLAG)){
			//无数据正常处理日志模式
			this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_INSERT);
		}		
		//已实例运行设置不能运行
		this.getPlmSchBS().txNewUpdateTaskExec(vo.getTaskCode(), PlmBasConstant.STOP_FLAG);
		param.setValue(PlmSchConstant.SOG_OBJECT, vo);
	}
	
	public final void taskAfterLog(GeneralParam param) {
		HpSogTaskVO vo = (HpSogTaskVO)param.getValueObject(PlmSchConstant.SOG_OBJECT);
		HpSchTaskVO schTaskVO = (HpSchTaskVO)param.getValueObject(PlmSchConstant.TASK_OBJECT);
		//无数据不处理日志模式
		if (param.getValueString(PlmSchConstant.TASK_HAS_DATA) != null && 
				param.getValueString(PlmSchConstant.TASK_HAS_DATA).equals(PlmBasConstant.USED_FLAG)){
			//有数据先插入后更新
			this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_INSERT);
		}
		vo.setTaskStatus(PlmSchConstant.TASK_RUN_SUCCESS);
		Object[] objs = new Object[] {vo.getTaskName()};
		String info = UtilMessage.getMessage(PlmSogConstant.MSG_0005, objs);
		vo.setRemark(info);
		if (schTaskVO.getIsDataLog().equals(PlmBasConstant.STOP_FLAG)){
			//无数据正常处理日志模式
			this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_UPDATE);
		}else{
			//无数据不处理日志模式
			if (param.getValueString(PlmSchConstant.TASK_HAS_DATA) != null && 
					param.getValueString(PlmSchConstant.TASK_HAS_DATA).equals(PlmBasConstant.USED_FLAG)){
				//有数据先插入后更新
				this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_UPDATE);
			}
		}
		
		//已实例运行设置可运行
		this.getPlmSchBS().txNewUpdateTaskExec(vo.getTaskCode(), PlmBasConstant.USED_FLAG);
	}
	
	public final void taskThrowLog(GeneralParam param, Exception ex) {
		HpSogTaskVO vo = (HpSogTaskVO)param.getValueObject(PlmSchConstant.SOG_OBJECT);
		HpSchTaskVO schTaskVO = (HpSchTaskVO)param.getValueObject(PlmSchConstant.TASK_OBJECT);
		if (schTaskVO.getIsDataLog().equals(PlmBasConstant.USED_FLAG)){
			//无数据不处理日志模式
			this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_INSERT);
		}
		vo.setTaskStatus(PlmSchConstant.TASK_RUN_FAIL);
		String info = null;
		Object[] objs = null;
		if(StringUtils.isBlank(ex.getMessage())){
			Throwable caseObj = ex.getCause();
			while(caseObj!=null){
				if(StringUtils.isBlank(caseObj.getMessage())){
					caseObj = caseObj.getCause();
				}else{
					break;
				}
			}
			if(caseObj!=null){
				objs = new Object[] {vo.getTaskName(), caseObj.getMessage()};
				info = UtilMessage.getMessage(PlmSogConstant.MSG_0006, objs);
			}else{
				objs = new Object[] {vo.getTaskName(), ex.getMessage()};
				info = UtilMessage.getMessage(PlmSogConstant.MSG_0006, objs);
			}
		}else{
			objs = new Object[] {vo.getTaskName(), ex.getMessage()};
			info = UtilMessage.getMessage(PlmSogConstant.MSG_0006, objs);
		}
		byte[] bytes = info.getBytes();
		if(bytes.length>=PlmBasConstant.SERVICE_MEMO_LENGTH){
			vo.setRemark(new String(bytes, 0, PlmBasConstant.SERVICE_MEMO_LENGTH));
		}else{
			vo.setRemark(info);
		}
		this.getPlmSogBS().txNewTaskLog(vo, PlmSogConstant.SOG_TASK_UPDATE);
		//已实例运行设置可运行
		this.getPlmSchBS().txNewUpdateTaskExec(vo.getTaskCode(), PlmBasConstant.USED_FLAG);
	}
	
	/**
	 * 执行计划任务
	 * @param param GeneralParam
	 * @return boolean
	 */	
	public abstract void doJob(GeneralParam param);
	
	/**
	 * 执行计划任务预处理
	 * @param param GeneralParam
	 * @return boolean
	 */	
	public abstract boolean checkJobPrev(GeneralParam param);
}
